/// <reference path="../../Resources/js/jquery/jquery.debug.js" />
/// <reference path="../DataType.debug.js" />
/// <reference name="MicrosoftAjax.debug.js" />


function LeafItem(node, basis) {
    LeafItem.initializeBase(this, [node, basis]);
    this.addChanged(Function.createDelegate(this, this.validate));
    this.addLoading(Function.createDelegate(this.getValueHandler(), this.getValueHandler().load));
    this.addUnloading(Function.createDelegate(this.getValueHandler(), this.getValueHandler().unload));
    $(this.getInputs()).focus(Function.createDelegate(this, this.raiseFocused));    
}

LeafItem.Label = "MForm-Lbl";

LeafItem.prototype = {
    getDataType: function () {
        /// <returns type="BM.DataType" >
        /// the item's data type
        /// </returns>
        return this.basis.get_dataType();
    },

    getValueHandlerType: function () {
        /// <returns type="String" >
        /// the value handler type used for the valuehandler factory
        /// </returns>
        if (this.vhType == null) {
            this.vhType = this.node.getAttribute("type");
        }

        return this.vhType;
    },

    getValueHandler: function () {
        /// <returns type="BM.ValueHandler" >
        /// the value handler of the item
        /// </returns>
        if (this.valueHandler == null) {
            this.valueHandler = new BM.ValueHandler[this.getValueHandlerType()](this);
        }
        return this.valueHandler;
    },

    getInput: function () {
        /// <returns domElement="true" >
        /// the input (when more, the first input) of the Leaf item
        /// </returns>
        return this.getValueHandler().getInput();
    },

    getInputs: function () {
        /// <returns type="jQuery">
        /// the inputs array of the Leaf item
        /// </returns>
        return this.getValueHandler().getInputs();
    },

    getLocale: function () {
        /// <returns type="String" >
        /// the locale of the leaf
        /// </returns>        
        return this.locale;
    },

    getLabels: function () {
        /// <returns type="jQuery" >
        /// all the labels of the input (all the nosed with 'MForm-Lbl' class
        /// </returns>
        return $(this.node).children("." + LeafItem.Label);
    },

    setValue: function (value, locale) {
        this.getValueHandler().setValue(value);
    },

    getValue: function () {
        return this.getValueHandler().getValue();
    },

    getDataTypeValue: function () {
        /// <returns>
        /// the value of the leaf item in the data type appropriate for the item's dataType
        /// </returns>
        if (BM.DataType.isBooleanDataType(this.getDataType())) {
            try {
                return Boolean.parse(this.getValue());
            } catch (e) {
                return null;
            }
        }

        if (BM.DataType.isDateDataType(this.getDataType())) {
            var format = this.getMForm().get_culturalContext().getFormat(this.getDataType());
            var date = Date.parseLocale(this.getValue(), format);
            return date;
        }

        if (BM.DataType.isNumberDataType(this.getDataType())) {
            var number = Number.parseLocale(this.getValue());
            return number;
        }

        return this.getValue();
    },

    setDataTypeValue: function (value) {
        if (BM.DataType.isBooleanDataType(this.getDataType())) {
            this.setValue(value.toString());
            return;
        }

        if (BM.DataType.isDateDataType(this.getDataType())) {
            var format = this.getMForm().get_culturalContext().getFormat(this.getDataType());
            this.setValue(value.localeFormat(format));
            return;
        }

        if (BM.DataType.isNumberDataType(this.getDataType())) {
            this.setValue(value.localeFormat('n'));
            return;
        }

        this.setValue(value);
    },

    focus: function () {
        if (this.getNode().style.display == 'none') return false;
        this.raiseFocused();
        var input = this.getInput();
        if ($(input).is("*:visible[disabled != 'disabled']")) {
            return MForm.focus(input);
        }
        return false;
    },

    validate: function () {
        if (this.isOptional() && this.getValue() == '') {
            this.raiseValidationSuccess([]);
            return true;
        }

        return this.validateSelf();
    },

    isDisabled: function () {
        return !this.getInputs().is(':enabled');
    },

    setDisabled: function () {
        this.getInputs().each(function () {
            this.disabled = true;
        });

    },

    setEnabled: function () {
        this.getInputs().each(function () {
            this.disabled = false;
        });
    },

    dispose: function () {
        LeafItem.callBaseMethod(this, 'dispose');
        delete this.valueHandler;
    }
}

LeafItem.registerClass("LeafItem", MItem);
